<!DOCTYPE html>
<html lang="zh-Hans-cn" ng-app="danmu.audit" ng-controller="MainCtrl">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap-theme.min.css" rel="stylesheet">
    <link href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
    <link href="/manage.css" rel="stylesheet">
    <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <script src="http://cdn.staticfile.org/angular-ui-bootstrap/0.14.3/ui-bootstrap-tpls.min.js"></script>
    <title>弹幕审核</title>
</head>

<body>
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container-fluid">
            <div class="navbar-header"><a class="navbar-brand" href="#">弹幕审核</a></div>
        </div>
    </nav>
    <div class="container-fluid">
        <div class="row">
            <uib-alert type="danger" ng-show="haveError">错误{{err.code}}：{{err.desc}}；建议刷新页面。</uib-alert>
            <div class="col-sm-12 col-md-12 main">
                <uib-accordion close-others="false">
                    <uib-accordion-group heading="管理信息" is-open="true">
                        <div class="container">
                            <uib-alert type="danger" ng-show="!isLogin">务必先填入房间信息再进行管理。</uib-alert>
                            <uib-alert type="success" ng-show="isLogin">你选择了{{room}}房间</uib-alert>
                            <div class="list-group" ng-show="!isLogin">
                                <a href="#" class="list-group-item" ng-class="{active: room == roomS.id}" ng-repeat="roomS in roomList" ng-click="initRoom(roomS.id)">{{roomS.display}} ({{roomS.id}})</a>
                            </div>
                            <form ng-submit="enterRoom(password)" ng-show="!isLogin">
                                <div class="input-group">
                                    <input type="password" class="form-control" placeholder="房间密码" ng-model="password">
                                    <span class="input-group-btn">
                                        <button class="btn btn-success" type="submit">确认</button>
                                    </span>
                                </div>
                            </form>
                        </div>
                    </uib-accordion-group>
                    <uib-accordion-group heading="弹幕审核" is-open="true">
                        <div class="container">
                            <div ng-repeat="danmu in danmus track by danmu.socketId" style="margin-top: 5px; margin-right: 5px; display: inline-block">
                                <div class="btn-group" uib-dropdown>
                                    <button id="split-button-{{$index}}" type="button" class="btn btn-success" ng-click="passDanmu($index, danmu.id, '')">{{danmu.text}}</button>
                                    <button type="button" class="btn btn-danger" ng-click="deleteDanmu($index, danmu.id, '')">删</button>
                                    <button type="button" class="btn btn-primary" uib-dropdown-toggle>
                                <span class="caret"></span>
                                <span class="sr-only">Split button!</span>
                              </button>
                                    <ul uib-dropdown-menu role="menu" aria-labelledby="split-button-{{$index}}">
                                        <li role="menuitem" class="disabled"><a href="#">{{danmu.hash}}</a></li>
                                        <li class="divider"></li>
                                        <li role="menuitem"><a href="#" ng-click="deleteDanmu($index, danmu.id, danmu.hash)">封</a></li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </uib-accordion-group>
                </uib-accordion>
            </div>
        </div>
    </div>
    <script src="/socket.io/socket.io.js"></script>

    <script>
var realtime = (function () {
var realtime = angular.module("danmu.audit", [
"ui.bootstrap",
"auditControllers"
]);
var auditControllers = angular.module('auditControllers', []);
var registerInit = []; // 用于初始化回调
var socket = null;

// alternatively, register the interceptor via an anonymous factory

realtime.config(['$httpProvider',
function ($httpProvider) {
$httpProvider.interceptors.push(function ($q, $rootScope) {
return {
'responseError': function (response) {
$rootScope.err.code = response.status;
$rootScope.err.desc = response.data.error;
$rootScope.haveError = true;
return $q.reject(response);
},
'response': function (response) {
$rootScope.err.code = response.status;
$rootScope.err.desc = "";
$rootScope.haveError = false;
return response;
}
};
});
}
]);

auditControllers.controller("MainCtrl",
function ($scope, $http, $rootScope) {
$scope.accordion = {
openInfo: true,
};
$scope.isLogin = false;
$scope.connectToServer = false;
$scope.room = "";
$scope.password = "";
$scope.danmus = [];
$scope.config = null;
$rootScope.haveError = false;
$rootScope.err = {
code: 200,
desc: ""
};

$scope.initRoom = function (room) {
$scope.room = room;
};
$scope.enterRoom = function (password) {
$scope.password = password;
for (var object in registerInit) registerInit[object].call();
$scope.isLogin = true;
$scope.accordion.openInfo = false;
};
$scope.buildParam = function (object) {
object.room = $scope.room;
object.password = $scope.password;
return object;
};
$scope.passDanmu = function ($index, id, blockHash) {
                socket.emit("auditPass", {
                    room: $scope.room, 
                    id: id 
                });
                $scope.danmus.splice($index, 1);
};
$scope.deleteDanmu = function ($index, id, blockHash) {
socket.emit("auditFail", {
                    room: $scope.room, 
                    id: id, 
                    hash: blockHash, 
                });
                $scope.danmus.splice($index, 1);
};

$http.post("/manage/room/get/", $scope.buildParam({})).success(function (data, status, headers, config) {
$scope.roomList = data;
});
registerInit.push(function () {
$http.post("/manage/config/password/get/", $scope.buildParam({})).success(function (data, status, headers, config) {
$scope.config = data;
socket = io(location.origin);
                    socket.on("connect", function() {
                        socket.emit("auditLogin", {
                            password: $scope.config.connectpassword,
                            room: $scope.room
                        });
                    });
socket.on("auditConnected", function () {
                        $scope.danmus = [];
                        $scope.$apply();
});
socket.on("auditDanmu", function (data) {
                        for (key in data) {
                            var value = data[key];
                            value.id = key;
                            value.socketId = value.id + "-" + socket.id;
$scope.danmus.push(value);
                        }
$scope.$apply();
});
});
});
}
);

return realtime;
})();
    </script>
</body>

</html>